package best.driver.android.Activity.Order.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import best.driver.android.Activity.Order.AcceptOrderActivity;
import best.driver.android.Activity.Order.OrderDetailActivity;
import best.driver.android.Activity.Order.OrderOperationActivity;
import best.driver.android.Beans.*;
import best.driver.android.BestApp;
import best.driver.android.R;
import best.driver.android.Utils.SizeUtils;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.manager.OrderManager;
import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.List;

public class ValidOrderItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private ArrayList<NewOrderInfo> mOrderItems = new ArrayList<NewOrderInfo>();

    public ValidOrderItemAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<NewOrderInfo> list) {
        mOrderItems.clear();
        mOrderItems.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mOrderItems.size();
    }

    @Override
    public NewOrderInfo getItem(int position) {
        return mOrderItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.item_order_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        setConvertView(holder, position);
        final NewOrderInfo orderInfo = getItem(position);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(BestApp.getInstance().getLastActivity() instanceof AcceptOrderActivity) {
                    ((AcceptOrderActivity) BestApp.getInstance().getLastActivity()).showOrderNew(orderInfo, PushType.NEW_ORDER);
                    return;
                }
                Intent acceptIntent = new Intent(BestApp.getInstance().getApplicationContext(), AcceptOrderActivity.class);
                acceptIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                acceptIntent.putExtra("orderId", orderInfo.orderId);
                acceptIntent.putExtra("new_order", orderInfo);
                acceptIntent.putExtra("status", PushType.NEW_ORDER);
                acceptIntent.putExtra("fromOrderList", true);
                mContext.startActivity(acceptIntent);
            }
        });
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        NewOrderInfo item = getItem(position);
        // if(OrderStatus.WAIT_START.equals(item.status)) {
        holder.mTvOrderTime.setText(TimeUtil.formatTime(item.bookTime));
        // } else {
        //     holder.mTvOrderTime.setText(TimeUtil.convertToMDHM(item.orderTime));
        // }
        List<OrderAddresses> addresses = item.orderAddresses;
        boolean showFirstShipper = false;
        if (addresses != null && addresses.size() > 0) {
            for (int i = 0; i < addresses.size(); i++) {
                OrderAddresses address = addresses.get(i);
                if (OrderStatus.SHIPPER_ADDRESS.equals(address.flag) && !showFirstShipper) {
                    holder.mTvOrderStartAddress.setText(address.address);
                    showFirstShipper = true;
                } else if (OrderStatus.RECEIVER_ADDRESS.equals(address.flag)) {
                    holder.mTvOrderDestination.setText(address.address);
                }
            }
        }
        if (OrderStatus.REAL_TIME.equals(item.type)) {
            holder.mTvOrderType.setBackgroundResource(R.drawable.circle_red_border);
            holder.mTvOrderType.setText("实时");
        } else {
            holder.mTvOrderType.setBackgroundResource(R.drawable.circle_blue_border);
            holder.mTvOrderType.setText("预约");
        }
        holder.mTvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.best_red));
        holder.mTvOrderStatus.setVisibility(View.GONE);
        holder.mIvPickup.setImageResource(R.drawable.icon_carry_n);
        holder.mIvUnload.setImageResource(R.drawable.icon_remove_n);
        holder.mTvOrderStartAddress.setTextColor(mContext.getResources().getColor(R.color.aaaa));
        holder.mTvOrderDestination.setTextColor(mContext.getResources().getColor(R.color.aaaa));


        holder.mTvPrePrice.setText("预估" + Util.getOnePointNumber(item.estimatePrice) + "元");

        if (item.familiarPrice > 0.1) {
            holder.mTvPrePrice.setText("一口价" + Util.getOnePointNumber(item.familiarPrice) + "元");
        }

        if (OrderStatus.ONE_TO_ONE.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        } else if (OrderStatus.ONE_TO_N.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.VISIBLE);
        } else if (OrderStatus.N_TO_ONE.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.VISIBLE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        } else {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        }


        holder.mTvUnloadMulti.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.circle_gray_yellow));
        holder.mTvUnloadMulti.setPadding((int) (8 * SizeUtils.getDensity(mContext)), 0, (int) (8 * SizeUtils.getDensity(mContext)), 0);
        holder.mTvOrderTime.setTextColor(mContext.getResources().getColor(R.color.aaaa));
        holder.mTvUnloadMulti.setTextColor(mContext.getResources().getColor(R.color.best_yellow));
    }

    static class ViewHolder {

        @InjectView(R.id.tv_order_type)
        TextView mTvOrderType;

        @InjectView(R.id.tv_order_time)
        TextView mTvOrderTime;

        @InjectView(R.id.tv_order_status)
        TextView mTvOrderStatus;

        @InjectView(R.id.tv_order_start_address)
        TextView mTvOrderStartAddress;

        @InjectView(R.id.tv_order_destination)
        TextView mTvOrderDestination;

        @InjectView(R.id.tv_pre_price)
        TextView mTvPrePrice;

        @InjectView(R.id.btn_operation)
        Button mBtnOrderOperation;

        @InjectView(R.id.tv_load_multi)
        TextView mTvLoadMulti;

        @InjectView(R.id.tv_unload_multi)
        TextView mTvUnloadMulti;

        @InjectView(R.id.iv_pick_up)
        ImageView mIvPickup;

        @InjectView(R.id.iv_unload)
        ImageView mIvUnload;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

}
